package com.huluobo.day11_im.addFriend.view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.day11_im.R;
import com.huluobo.day11_im.addFriend.presenter.AddFriendContract;
import com.huluobo.day11_im.addFriend.presenter.AddFriendPresenter;

public class AddFriendActivity extends AppCompatActivity implements AddFriendContract.View {
    private ImageView back;

    private EditText username, reason;
    private Button toAddBtn;
    private TextView title;
    private AddFriendPresenter addFriendPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        title = findViewById(R.id.main_title);
        title.setText(getResources().getString(R.string.addFriend));
        username = findViewById(R.id.addFriend_username);
        reason = findViewById(R.id.addFriend_reason);
        addFriendPresenter = new AddFriendPresenter(this, this);
        toAddBtn = findViewById(R.id.addFriend_add_btn);

        back = findViewById(R.id.header_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toAddBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addFriendPresenter.add(username.getText().toString(), reason.getText().toString());
            }
        });
    }

    @Override
    public void onAddSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddFriendActivity.this, "添加好友成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onAddFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddFriendActivity.this, "添加好友失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}